#include "UtilityPCH.h"

#include "UT_CameraBase.h"
#include "UT_Log.h"
#include "UT_MathHelper.h"

using namespace  Utility;
//---------------------------------------------------------------
UT_CameraBase::UT_CameraBase(const UT_AnimatedTransform &cam2world,float sopen, float sclose, UT_Film *f)
	: m_kCameraToWorld(cam2world), m_fShutterOpenTime(sopen), m_fShutterCloseTime(sclose)
{
	m_pkFilm = f;
	if (m_kCameraToWorld.HasScale())
	{
		GfxWriteLog(LOG_S_DEFAULT_ERROR,"Camera have scale transformation");
	}
}
//---------------------------------------------------------------
UT_CameraBase::~UT_CameraBase()
{
	MM_SAFE_DELETE(m_pkFilm);
}
//---------------------------------------------------------------
float UT_CameraBase::GenerateRayDifferential(const UT_CameraSample &sample,	UT_RayDifferential *rd) const
{
	float wt = GenerateRay(sample, rd);
	// Find ray after shifting one pixel in the $x$ direction
	UT_CameraSample sshift = sample;
	++(sshift.m_fImageX);
	
	UT_Ray rx;
	float wtx = GenerateRay(sshift, &rx);
	rd->rxOrigin = rx.o;
	rd->rxDirection = rx.d;

	// Find ray after shifting one pixel in the $y$ direction
	--(sshift.m_fImageX);
	++(sshift.m_fImageY);

	UT_Ray ry;
	float wty = GenerateRay(sshift, &ry);
	rd->ryOrigin = ry.o;
	rd->ryDirection = ry.d;
	
	if (wtx == 0.f || wty == 0.f) 
		return 0.f;

	rd->hasDifferentials = true;

	return wt;
}
//---------------------------------------------------------------
//---------------------------------------------------------------
//---------------------------------------------------------------
